using Gx.Common;
using Gx.Common.Core;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{
    public class LKGoodsBll
    {
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public PagedList<LKGoods> GetLKGoodsPageList(GoodsInput input)
        {
            LKGoodsDal dal = new LKGoodsDal();
            return dal.GetPagedList(input);
        }
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <returns></returns>
        public List<LKGoods> GetLKGoodss()
        {
            BaseDal<LKGoods> dal = new BaseDal<LKGoods>();
            return dal.GetAll().ToList();
        }

        /// <summary>
        /// 新增商品
        /// </summary>
        /// <param name="lKGoods"></param>
        /// <returns></returns>
        public ReturnModel AddLKGoods(LKGoods lKGoods)
        {
            LKGoodsDal dal = new LKGoodsDal();

            var any = dal.AnyGoodsName(lKGoods.GoodsName);
            if (any)
            {
                return new ReturnModel { Success = false, Msg = "商品名称已存在" };
            }

            lKGoods.GoodsCode = GetGoodsCode();
            lKGoods.CreateTime = DateTime.Now;
            lKGoods.CreateUserId = LoginHelper.GetUserId();
            lKGoods.CreateUser = LoginHelper.UserName();
            var result= dal.Insert(lKGoods);
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }
        private string GetGoodsCode()
        {
            LKGoodsDal dal = new LKGoodsDal();
            var maxCode = dal.GetMaxGoodsCode();
            if (string.IsNullOrWhiteSpace(maxCode))
            {
                return "100000";
            }
            return (int.Parse(maxCode) + 1).ToString();
        }

        /// <summary>
        /// 编辑商品
        /// </summary>
        /// <param name="lKGoods"></param>
        /// <returns></returns>
        public bool EditLKGoods(LKGoods lKGoods)
        {
            BaseDal<LKGoods> dal = new BaseDal<LKGoods>();
            var cus = dal.Get(lKGoods.Id);
            if (cus == null)
            {
                return false;
            }
            lKGoods.CreateTime = cus.CreateTime;
            lKGoods.CreateUserId = cus.CreateUserId;
            lKGoods.CreateUser = cus.CreateUser;
            lKGoods.ModifyUserId = LoginHelper.GetUserId();
            lKGoods.ModifyUser = LoginHelper.UserName();
            lKGoods.ModifyTime = DateTime.Now;
            return dal.Update(lKGoods);
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteLKGoods(int id)
        {
            BaseDal<LKGoods> dal = new BaseDal<LKGoods>();
            return dal.DeleteById(id);
        }
    }
}

